package dylan.id.final1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FinalActivity extends Activity {
	@SuppressWarnings("unused")
	private int counter = 0;
	private MediaRecorder recorder;
	private boolean recording  = false;
	private boolean p1went = false;
	private int p1Amp = 0;
	private int p2Amp = 0;
	private boolean gameFinish = false;
	@SuppressWarnings("unused")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Bundle bundle = getIntent().getExtras();
		String sender = bundle.getString("text");
		counter = Integer.parseInt(sender);
		final TextView winner = (TextView)findViewById(R.id.textView1);
		final TextView winHistory = (TextView)findViewById(R.id.textView2);
		winner.setText("Player 1's Turn");
		final Button a = (Button)findViewById(R.id.button1);
		a.setText("Start");
		a.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(counter > 0){
					if(gameFinish == true){
						gameFinish = false;
						p1went = false;
						recording = false;
						winner.setText("Player 1's Turn");
						a.setText("Start");
					}
					else if(recording == false && p1went == false){
						recording = true;
						start();
						recorder.getMaxAmplitude();
						p1went = false;
						winner.setText("Player 1 is Recording");
						a.setText("Stop");
					}
					else if(recording == true && p1went == false){
						p1went = true;
						winner.setText("Player 2's Turn");
						a.setText("Start");
						recording = false;
						p1Amp = recorder.getMaxAmplitude();
						stop();
					}
					else if(recording == false && p1went == true){
						recording = true;
						start();
						recorder.getMaxAmplitude();
						winner.setText("Player 2 is Recording");
						a.setText("Stop");
					}
					else if(recording == true && p1went == true){
						recording = false;
						p2Amp = recorder.getMaxAmplitude();
						stop();
						gameFinish = true;
						String whoWins = "";
						if(p2Amp > p1Amp){
							whoWins = "Player 2 wins!";
						}
						if(p2Amp < p1Amp){
							whoWins = "Player 1 wins!";
						}
						a.setText("Stop");
						String t = winHistory.getText().toString();
						t+="\n" + whoWins;
						winHistory.setText(t);
						winHistory.setMovementMethod(new ScrollingMovementMethod());
						counter--;
					}
				}
			}
		});
	}
	public void start() {
		if(recorder == null){
			recorder = new MediaRecorder();
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);	
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			/*File dir = getFilesDir();
			dir.mkdirs();
			Log.i("app", Environment.getExternalStorageState());
			File file  = new File(dir.getAbsolutePath() + "/audio.3gp" );
			recorder.setOutputFile(file.getAbsolutePath());*/
			try {
				FileOutputStream fos = openFileOutput("audio.3gp", Context.MODE_PRIVATE);
				recorder.setOutputFile(fos.getFD());
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				recorder.prepare();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			recorder.start();
		}
	}
	public void stop(){
		if(recorder != null){
			recorder.stop();
			recorder.release();
			recorder = null;
		}
	}
}